package com.qf.commons.event.core.handle;

import com.qf.commons.event.apply.consumer.EventHandler;
import com.qf.commons.event.apply.consumer.IEventHandler;
import com.qf.commons.event.core.standrad.ConsumerInterceptor;
import com.qf.commons.event.core.standrad.ConsumerStandrad;
import com.qf.commons.event.entity.QphoneMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 消费端消息接收的默认处理类
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Component
public class DefaultConsumerStandrad implements ConsumerStandrad {

    @Autowired(required = false)
    private List<ConsumerInterceptor> interceptors;

    @Autowired(required = false)
    private List<IEventHandler> eventHandlers;

    @Override
    public void msgHandler(final QphoneMessage qphoneMessage) {

        //设置消息的接收时间
        qphoneMessage.setRecivTime(new Date());

        //获取消息处理器
        IEventHandler eventHandler = findHandler(qphoneMessage);
        if (eventHandler == null) return;

        //获取当前处理器的注解
        EventHandler annotation = AnnotationUtils.findAnnotation(eventHandler.getClass(), EventHandler.class);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                QphoneMessage temMsg = qphoneMessage;

                //拦截器的处理
                if (!CollectionUtils.isEmpty(interceptors)) {
                    //循环拦截器链
                    for (ConsumerInterceptor interceptor : interceptors) {
                        try {
                            if (!interceptor.isSupport(temMsg)) continue;
                            //拦截器处理消息
                            temMsg = interceptor.msgPostProcessor(temMsg);
                            //判断处理的结果
                            if (qphoneMessage == null) return;
                        } catch (Exception e) {
                            //日志记录异常
                        }
                    }
                }

                //调用上层接口，处理消息
                eventHandler.handler(temMsg.getEventContent(), temMsg);
            }
        };

        if (annotation.isAsync()) {
            //多线程
            new Thread(runnable).start();
        } else {
            runnable.run();
        }
    }

    /**
     * 通过消息，获取能够处理这个消息的实现类
     * @param qphoneMessage
     * @return
     */
    private IEventHandler findHandler(QphoneMessage qphoneMessage){
        if (CollectionUtils.isEmpty(eventHandlers)) return null;

        //循环每个消息处理器，看事件类型是否匹配
        for (IEventHandler eventHandler : eventHandlers) {
            EventHandler annotation = AnnotationUtils.findAnnotation(eventHandler.getClass(), EventHandler.class);
            if (annotation.value().equals(qphoneMessage.getEventType())) {
                //当前处理器可以处理该消息
                return eventHandler;
            }
        }
        return null;
    }
}
